Ext.define('App.crm.paymentRecordsForm', {
    extend: 'Ext.window.Window',
    alias: 'widget.paymentRecordsForm',
    initComponent: function () {
        var form = Ext.widget('form', {
            frame: false,
            border: true,
            split: true,
            autoScroll: true,
            bodyPadding: 5,
            bodyBorder: true,
            fieldDefaults: {
                labelWidth: 90,
                labelAlign: 'right',
                msgTarget: 'side'
            },
            defaults: {
                anchor: '100%'
            },
            items: [
                {xtype: 'hidden',
                    name: 'ownerId',
                    dataIndex: 'ownerId'
                },
                {xtype: 'hidden',
                    name: 'id',
                    dataIndex: 'id'
                },
                {xtype: 'hidden',
                    name: 'deleteFlag',
                    dataIndex: 'deleteFlag'
                },
                {
                    xtype: 'hidden',
                    name: 'customersId',
                    dataIndex: 'customersId'
                },
                {
                    xtype: 'fieldset',
                    title: '开票记录信息',
                    collapsible: true,
                    defaults: {
                        anchor: '100%',
                        layout: {
                            type: 'hbox',
                            defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                        }
                    },
                    items: [

                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [
                            {
                                xtype: 'container',
                                layout: 'hbox',
                                margin: '0 0 5 0',
                                defaults: {
                                }, items: [
                                {xtype: 'hidden',
                                    name: 'contractsId',
                                    dataIndex: 'contractsId'
                                },
                                {xtype: 'textfield',
                                    name: 'contractsName',
                                    dataIndex: 'contractsName',
                                    fieldLabel: '合同',
                                    readOnly: true,
                                    emailText: '请选择合同',
                                    width: 240,
                                    allowBlank: false
                                },
                                {
                                    xtype: 'button',
                                    name: 'btncontractsId',
                                    iconCls: 'searchIcon',
                                    scope: this,
                                    handler: this.onContractSel,
                                    width: 25
                                }
                            ]},
                            {xtype: 'textfield',
                                name: 'customersName',
                                dataIndex: 'customersName',
                                fieldLabel: '客户名称',
                                readOnly: true
                            }
                        ]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [  new comboBox({
                            name: 'period',
                            dataIndex: 'period',
                            fieldLabel: '期次',
                            labelAlign: 'right',
                            codeName: '合同期次',
                            queryMode: 'local',
                            typeAhead: false,
                            forceSelection: true,
                            loadingText: '正在搜索...',
                            allowBlank: true,
                            hideTrigger: false
                        })
                            , {xtype: 'textfield',
                                name: 'foreignCurrent',
                                dataIndex: 'foreignCurrent',
                                fieldLabel: '外币备注',
                                allowBlank: true
                            }
                        ]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [  new dateTime({
                            name: 'reimdate',
                            dataIndex: 'reimdate',
                            format: 'Y-m-d',
                            fieldLabel: '实际回款日期',
                            allowBlank: true
                        }),
                            , {xtype: 'numberfield',
                                name: 'amount',
                                dataIndex: 'amount',
                                fieldLabel: '实际回款金额',
                                allowBlank: true
                            }
                        ]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [  new dateTime({
                            name: 'planReimdate',
                            dataIndex: 'planReimdate',
                            format: 'Y-m-d',
                            fieldLabel: '计划回款日期',
                            allowBlank: true
                        }),
                            , {xtype: 'numberfield',
                                name: 'planAmount',
                                dataIndex: 'planAmount',
                                allowDecimals: true,
                                decimalPrecision: 2,
                                fieldLabel: '计划回款金额',
                                allowBlank: true
                            }
                        ]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [  new comboBox({
                            name: 'invoice',
                            dataIndex: 'invoice',
                            fieldLabel: '已开发票',
                            labelAlign: 'right',
                            codeName: '已开发票',
                            queryMode: 'local',
                            typeAhead: false,
                            forceSelection: true,
                            loadingText: '正在搜索...',
                            allowBlank: true,
                            hideTrigger: false
                        })
                            , new comboBox({
                                name: 'paymentMethods',
                                dataIndex: 'paymentMethods',
                                fieldLabel: '付款方式',
                                labelAlign: 'right',
                                codeName: '付款方式',
                                queryMode: 'local',
                                typeAhead: false,
                                forceSelection: true,
                                loadingText: '正在搜索...',
                                allowBlank: true,
                                hideTrigger: false
                            })
                        ]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [
                            {xtype: 'textfield',
                                name: 'ownerName',
                                dataIndex: 'ownerName',
                                fieldLabel: '收款人',
                                allowBlank: true
                            }
                            ,
                            new comboBox({
                                name: 'classification',
                                dataIndex: 'classification',
                                fieldLabel: '分类',
                                labelAlign: 'right',
                                codeName: '回款记录分类',
                                queryMode: 'local',
                                typeAhead: false,
                                forceSelection: true,
                                loadingText: '正在搜索...',
                                allowBlank: true,
                                hideTrigger: false
                            })
                        ]}
                    ]},
                {
                    xtype: 'fieldset',
                    title: '备注信息',
                    collapsible: true,
                    defaults: {
                        anchor: '100%',
                        layout: {
                            type: 'hbox',
                            defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                        }
                    },
                    items: [
                        {xtype: 'textarea',
                            name: 'remark',
                            dataIndex: 'remark',
                            fieldLabel: '备注',
                            allowBlank: true
                        }
                    ]}
            ]
        });
        Ext.apply(this, {
            title: '新增',
            layout: 'fit',
            height: 500,
            width: 600,
            modal: true,
            constrain: true,
            closeAction: 'hide',
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        //this.up(‘window’)获取了顶级的window组件。接着使用down()方法获取了window的子组件form组件，最后使用getForm()来获取整个form。
                        var form = this.up('paymentRecordsForm').down('form');
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/paymentRecordsController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    this.up('paymentRecordsForm').close();
                                    Ext.getCmp('paymentRecordsGrid').getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    }
                },
                {
                    text: "关闭",
                    handler: function () {
                        this.up('paymentRecordsForm').close();
                    }
                }
            ],
            items: form
        });

        this.callParent(arguments);
    }, contractSelWin: null,
    onContractSel: function () {
        if(!this.contractSelWin){
            this.contractSelWin= new contractsSel();
            this.contractSelWin.on('onSelectItem', function(data) {
                this.down('form').getForm().findField("customersId").setValue(data.customers.id);
                this.down('form').getForm().findField("customersName").setValue(data.customers.company);
                this.down('form').getForm().findField("contractsId").setValue(data.id);
                this.down('form').getForm().findField("contractsName").setValue(data.name);
            }, this);

        }
        this.contractSelWin.show();
    }
});